﻿using DevExpress.XtraPrinting.BarCode;
using DevExpress.XtraReports.UI;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;

namespace Tailord
{
    public partial class SiparisFormu : DevExpress.XtraReports.UI.XtraReport
    {
        DataSet dsFoy;
        List<string[]> Captions = new List<string[]>();
        int LanguageIndex = 0;

        string[] CurrentCaptions
        {
            get
            {
                return Captions[LanguageIndex];
            }
        }

        public SiparisFormu(int languageIndex, DataSet dsFoy)
            : this(dsFoy)
        {
            LanguageIndex = languageIndex;
            Captions.Add("Sipariş Formu,Müşteri,Tel,Siparişi Alan,Sipariş Tarihi,Teslim Tarihi,Ölçüler,Nakış Bilgileri,Yaka,Göğüs,Bel,Bel Oturur,Basen,Ön Boy,Omuz,SOL,SAĞ,Kol Boyu,Bilek,Pazu,Ön Kol,Ön Ayna,Arka Ayna,Özel Not,Nakış Yazısı,Nakış Yeri,Propozisyonlar,Duruş,Omuz,Nakış Karakteri,Paketleme,Sipariş Formu,Adet,Kumaş Kodu,Yaka,Pat,Sırt,Mnş.,Cep,Opsiyon,Dğm.,Dikim,Dğm Ö.,Nakış R.,Garni1 - İç,Garni 1 Poz.,Garni 2 - Dış,Garni 2 Poz.,Fit Kodu,Prova,Toplam Fiyat,İndirim,Net Fiyat,Açıklamalar,Müşteri Kodu,Satıcı/Kod,Mağaza/Kod,Fatura Kime, Ödeme Türü, Kampanya,Kapora Belge No,Kapora Tutarı,Takip No, Mağaza/Kod,Baz Kalıp/Beden,Kampanya".Split(','));
            Captions.Add("Order Report,Customer,Phone,Ordered By,Order Date,Delivery Date,Measurements,Embroidery Details,Collar,Chest,Waist,Waist / Sitted,Seat,Shirt Length,Shoulder,LEFT,RIGHT,Sleeve Length,Wrist,Biceps,Front Arm,Front Chest,Blade,Notes,Embroidery Initials,Embroidery Pos.,Propozitions,Posture,Shoulders,Embroidery Font,Packing,Order Report,Qty,Fabric Code,Collar,Plck.,Back,Cuff,Pocket,Optional,Btn.,Sewing,Btn S.,Embr. Col.,Inner Trim,Inner Trim Pos.,Outer Trim,Outer Trim Pos.,Fit Code,Trial,Total Amount,Discount,Net Amount,Notes2,Customer Code,Seller/Code,Store/Code,Invoice,Payment Type,Campaign,Down Payment Doc.,Down Payment,Track Nr.,Store/Code,Base Fit/Size,Campaign".Split(','));

            for (int i = 1; i < CurrentCaptions.Length + 1; i++)
            {
                string caption = CurrentCaptions[i - 1];
                ((XRTableCell)this.FindControl("tc" + (i).ToString(), false)).Text = caption;
            }
            tc1.Text += " - No." + dsFoy.Tables[0].Rows[0]["OrderId"].ToString();
            tc32.Text += " - No." + dsFoy.Tables[0].Rows[0]["OrderId"].ToString();
            int firmaId = (int)dsFoy.Tables["Table"].Rows[0]["FirmaId"];


            if (firmaId == 11)
            {
                tbResmiOnay.Visible = tbKapora.Visible = tbTakip.Visible = true;
                tbKapora.LocationF = tbFatura.LocationF;
                tbTakip.LocationF = tbSatici.LocationF;
            }

            tbFatura.Visible = trIndirim.Visible = trNetFiyat.Visible = firmaId == 5 || firmaId == 14;
            tbBazKalip.Visible = firmaId == 5 || firmaId == 14 || firmaId == 17 || firmaId == 2;
            tbBarkod.Visible = firmaId == 11 || firmaId == 29 || firmaId == 31;
            tbSatici.Visible = firmaId == 2 || firmaId == 29 || firmaId == 31;
            tbKampanya.Visible = firmaId == 2;
        }

        public SiparisFormu(DataSet dsFoy)
        {
            InitializeComponent();

            foreach (DataTable dt in dsFoy.Tables)
                foreach (DataRow dr in dt.Rows)
                    foreach (DataColumn dc in dt.Columns)
                    {
                        if (dc.DataType == typeof(decimal))
                        {
                            decimal val = 0;
                            decimal.TryParse(dr[dc].ToString(), out val);
                            if (val == 0)
                                dr[dc] = DBNull.Value;
                        }
                    }


            this.dsFoy = dsFoy;

            NakisSub subreportNakis = (NakisSub)srNakis.ReportSource;
            subreportNakis.fillTable(dsFoy.Tables["Table2"]);

            this.DataMember = "Table1";
            this.DataSource = dsFoy;


            XRTable[] tbarray = new XRTable[] { tb1,tb3,tbSatici, tb4, tb5, tb6, xrTable7, tb8, xrTable9, xrTable1, xrTable6, xrTable8 ,tbFiyat,tbFatura,tbKapora,tbTakip,tbBazKalip,tbKampanya};

            foreach (XRTable table in tbarray)
            {
                foreach (XRTableRow row in table.Rows)
                {
                    foreach (XRTableCell cell in row)
                    {
                        if (cell.Name.Length > 4)
                        {
                            cell.DataBindings.Add("Text", null, "Table." + cell.Name);
                        }
                    }
                }
            }

            
            
            //string path = string.Format(System.Web.HttpContext.Current.Server.MapPath("~/img/{0}.png"), dsFoy.Tables["Table"].Rows[0]["logo"].ToString());
            string path = string.Format("d:\\img\\{0}.png", dsFoy.Tables["Table"].Rows[0]["logo"].ToString());
            xrLogo1.ImageUrl = path;
            xrLogo2.ImageUrl = path;
        }

        private void Detail_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            this.DataMember = "Table1";
            this.DataSource = dsFoy;

            foreach (XRTableCell cell in trDetail.Cells)
            {
                cell.DataBindings.Add("Text", null, "Table1." + cell.Name);
            }

            //detailProva2.DataBindings.Add("CheckState", null, "Table1.detailProva2");
            //imgDugme.DataBindings.Add("ImageUrl", null, "Table1.detailDikim", "d:\\img\\dugme{0}.jpg" );
            //imgDugme.DataBindings.Add("ImageUrl", null, "Table1.detailDikim", System.Web.HttpContext.Current.Server.MapPath("~/img/dugme{0}Foy.jpg"));

            //tcTeslimTarihi.DataBindings.Add("Text",null,"Table1.tcTeslimTarihi", "{0:dd.MM.yyyy}");
        }

        private void srNakis_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ((NakisSub)((XRSubreport)sender).ReportSource).OrdId.Value = Convert.ToInt32(GetCurrentColumnValue("OrderId"));
        }

        private void ReSize(object sender)
        {
            //Resize ı düzgün yapan tek fonksiyon !!!
            Graphics gr = Graphics.FromHwnd(IntPtr.Zero);
            gr.PageUnit = GraphicsUnit.Millimeter;
            int factor = 10;

            XRLabel label = (XRLabel)sender;

            float newSize = 9.75f;
            Font defaultFont = new System.Drawing.Font("Calibri", newSize);

            float stringWidth = gr.MeasureString(label.Text, defaultFont).Width;
            float labelWidth = label.WidthF;


            if (labelWidth < stringWidth * factor)
                newSize = defaultFont.Size * label.Width / stringWidth / factor;


            label.Font = new Font(defaultFont.FontFamily, newSize);
            gr.Dispose();
        }

        private void detailKumaskodu_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ReSize(sender);
        }

        private void tcGomlekAdet_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ReSize(sender);
        }

        private void ReportFooter_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {

            foreach (DataRow dr in dsFoy.Tables[3].Rows)
            {
                XRTableRow tr = new XRTableRow();

                XRTableCell tcNo = new XRTableCell();
                XRTableCell tcBarkod = new XRTableCell();
                XRTableCell tcEan = new XRTableCell();
                XRTableCell tcAdet = new XRTableCell();

                XRBarCode br = new XRBarCode();
                Code128Generator code128Generator = new Code128Generator();

                tcNo.Text = dr["Nr"].ToString(); 
                tcNo.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter;
                tcNo.WidthF = 72.36F;


                br.Dpi = 254F;
                br.Module = 3.08F;
                br.Padding = new DevExpress.XtraPrinting.PaddingInfo(25, 25, 0, 20, 254F);
                br.SizeF = new System.Drawing.SizeF(719.82F, 75F);
                br.Symbology = code128Generator;
                br.Text = dr["Barkod"].ToString();
                br.ShowText = false;

                tcBarkod.Controls.Add(br);
                tcBarkod.Font = new System.Drawing.Font("Calibri", 9.75F, System.Drawing.FontStyle.Regular);
                tcBarkod.Multiline = true;
                tcBarkod.WidthF = 719.82F;                
                tcBarkod.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter;

                tcEan.Text = dr["Barkod"].ToString();
                tcEan.WidthF = 283.26F;
                tcEan.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter;

                tcAdet.Text = dr["Adet"].ToString();
                tcAdet.WidthF = 81.7F;
                tcAdet.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter;

                tr.Cells.AddRange(new XRControl[] { tcNo, tcBarkod, tcEan, tcAdet });
                tbBarkod.Rows.Add(tr); 
            }
        }
        
        private void tbResmiOnay_PrintOnPage_1(object sender, PrintOnPageEventArgs e)
        {
            if (e.PageCount > 0)
            {
                if (e.PageIndex != e.PageCount - 1)
                {
                    e.Cancel = true;
                    //this.xrTable4.Dispose();
                }
            }
        }
    }
}